package service;

import entity.Manager;
import function.Druid;
import function.ManagerDaoImpl;
import interfaces.ManagerServiceDao;
import tool.MyTool;
import tool.TSUtility;
import view.LogView;

import java.util.List;
import java.util.Scanner;

public class ManagerServiceImpl implements ManagerServiceDao {
    Scanner sc = new Scanner(System.in);
    ManagerDaoImpl managerDao = ManagerDaoImpl.getManagerDao();


    private static ManagerServiceImpl managerService = null;

    private ManagerServiceImpl() {

    }

    public static ManagerServiceImpl getManagerService() {
        if (managerService == null)
            managerService = new ManagerServiceImpl();
        return managerService;
    }

    @Override
    public boolean insert() throws Exception {
        System.out.println("请输入管理员的用户名(直接按确认退出)");
        String name = MyTool.getSixString(true, "");
        if (name.equals("")) {
            System.out.println("退出成功");
            return false;
        }
        System.out.println("请输入管理员的密码：");
        String password = MyTool.getSixString(false, null);
        System.out.println("是否添加（Y/N):");
        char c = TSUtility.readConfirmSelection();
        if (c == 'N') return false;
        Manager manager = new Manager(name, password);
        boolean b = managerDao.insertOperator(Druid.getConnection(), manager);
        if (b) {
            Journal.record("注册了管理员",name);
            System.out.println("注册成功");
            return true;
        }{
            System.out.println("注册失败");
            return false;}
    }

    @Override
    public void delete() throws Exception {
        System.out.println("请输入你要删除的管理者id(直接按确定退出)：");
        Integer id = MyTool.readInt();
        if (id == null) return;
        System.out.println("是否删除？（Y/N）");
        char c = TSUtility.readConfirmSelection();
        if (c == 'N') return;
        Manager manager = managerDao.selectById(Druid.getConnection(), id);
        boolean b = managerDao.deleteOperator(Druid.getConnection(), id);
        if (b) {
            System.out.println("删除成功");
            Journal.record("删除了管理员",manager.getName());
        }
        else System.out.println("删除失败");
    }

    @Override
    public void update() throws Exception {
        List<Manager> managers = selectAll();
        System.out.println("请输入你要修改的管理员的序号（直接按确认退出）：");
        Integer id = MyTool.getTypeIndex(managers.size());
        if (id == null) return;
        Manager manager = managers.get(id);
        System.out.println("用户名（" + manager.getName() + "):");
        String name = MyTool.getSixString(true, manager.getName());
        System.out.println("密码（" + manager.getPassword() + "):");
        String password = MyTool.getSixString(true, manager.getPassword());
        Manager manager1 = new Manager(manager.getId(),name, password);
        System.out.println("是否修改（Y/N）？");
        char c = TSUtility.readConfirmSelection();
        if(c=='N'){
            return;
        }
        boolean b = managerDao.updateOperator(Druid.getConnection(), manager1);
        if (b) {
            Journal.record("修改了",manager.getName(),"的信息");
            System.out.println("修改成功");
        }
        else System.out.println("修改失败");
    }

    @Override
    public Manager selectById() throws Exception {
        System.out.println("请输入你要查询的管理者的id（直接按确认退出）");
        Integer id = MyTool.readInt();
        if (id == null) return null;
        Manager manager = managerDao.selectById(Druid.getConnection(), id);
        if (manager != null) MyTool.printOne(manager);
        else {
            System.out.println("没有相关结果");
            return null;
        }
        Journal.record(LogView.getManager().getName(),"通过ID查询了",manager.getName(),"的信息");
        return manager;
    }


    @Override
    public List<Manager> selectAll() throws Exception {
        List<Manager> managers = managerDao.selectAll(Druid.getConnection());
        if (managers != null) MyTool.printArr(managers);
        else {
            System.out.println("没有相关结果");
            Journal.record("查询了所有管理员");
        }
        return managers;
    }

    public static void main(String[] args) {
        ManagerServiceImpl managerService = new ManagerServiceImpl();
        try {
            managerService.update();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
